準備技術:
從Java Web Server,可以觀察Java技巧,Web Server的原理,網路程式設計,最重要的是要了解HTTP的原理,這是Web Server的基本。
主程式,簡單的Java 應用程式,呼叫JavaHTTPServer 啟動Server,然後進入await方法維持上線狀態。
public class JavaHttpServer {
private static final String SHUTDOWN_COMMAND = "/SHUTDOWN";
private boolean shutdown = false;
public static void main(String[] args) {
JavaHttpServer server = new JavaHttpServer();
server.await();
}
}
await方法如下,首先設定一個ServerSocket,設定主機IP(hostIP)和Port,ServerSocket綁定主機IP,接下來While迴圈的!shutdown參數,是讓Server維持上線狀態,迴圈內容是設定一個Socket透過InputStream/OutStream的JavaIO方法,啟動SeverSocket,這樣Web Server有接收輸出資料的能力。然後我們要處理Request/Response ,如果需求是Servlet,需要ServletProcessor,如果是靜態html,需要StaticResourceProcessor。
public void await() {
ServerSocket serverSocket = null;
String hostIP="127.0.0.1";
int port = 10000;
try {
serverSocket = new ServerSocket(port, 1, InetAddress.getByName(hostIP));
System.out.println("Java Web Server start Host at "+hostIP+" and port="+port);
}
catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
while (!shutdown) {
Socket socket = null;
InputStream input = null;
OutputStream output = null;
try {
socket = serverSocket.accept();
input = socket.getInputStream();
output = socket.getOutputStream();
Request request = new Request(input);
request.parse();
Response response = new Response(output);
response.setRequest(request);
//假如是Servlet
if (request.getUri().startsWith("/servlet/")) {
ServletProcessor processor = new ServletProcessor();
processor.process(request, response);
}
else {
//假如是html靜態程式
StaticResourceProcessor processor = new StaticResourceProcessor();
processor.process(request, response);
}
socket.close();
shutdown = request.getUri().equals(SHUTDOWN_COMMAND);
}
catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}
看ServletProcessor處理Request/Response,呼叫process方式來處理。我們先將request的URL拆開,設定一個字串repository後使用URLClassLoader 載入一個class,我們希望將這個class轉型成Servlet,這樣可以使用Servlet的HTTPServletRequest/Response來當Resquest/Response的資料處理。
public class ServletProcessor1 {
public void process(Request request, Response response) {
String uri = request.getUri();
String servletName = uri.substring(uri.lastIndexOf("/") + 1);
URLClassLoader loader = null;
try {
URL[] urls = new URL[1];
URLStreamHandler streamHandler = null;
File classPath = new File(Constants.WEB_ROOT);
String repository = (new URL("file", null, classPath.getCanonicalPath() + File.separator)).toString() ;
urls[0] = new URL(null, repository, streamHandler);
loader = new URLClassLoader(urls);
}
catch (IOException e) {
System.out.println(e.toString() );
}
Class myClass = null;
try {
myClass = loader.loadClass(servletName);
}
catch (ClassNotFoundException e) {
System.out.println(e.toString());
}
Servlet servlet = null;
try {
servlet = (Servlet) myClass.newInstance();
servlet.service((ServletRequest) request, (ServletResponse) response);
}
catch (Exception e) {
System.out.println(e.toString());
}
catch (Throwable e) {
System.out.println(e.toString());
}
}
}
看StaticResourceProcessor,單純處理Request/Response。
public class StaticResourceProcessor {
public void process(Request request, Response response) {
try {
response.sendStaticResource();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
啟動Web Server。
Day1 結束